package pwd.allen.androiddemo.activity;

import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import pwd.allen.androiddemo.R;
import pwd.allen.androiddemo.fragment.DynamicFragment;
import pwd.allen.androiddemo.fragment.SelectFragment;


public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);

        fragmentManager = getSupportFragmentManager();
    }

    @Override
    public void onClick(View v) {
        Fragment fragment = null;
        switch (v.getId()) {
            case R.id.button1:
                fragment = SelectFragment.newInstance("内容");
                break;
            case R.id.button2:
                fragment = DynamicFragment.newInstance(0, R.drawable.huawei, "华为手机");
                break;
        }

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frameLayout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
